package com.example.musiccommunity.UserPage;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TableLayout;
import android.widget.TextView;

import androidx.fragment.app.Fragment;

import com.example.musiccommunity.MainActivity;
import com.example.musiccommunity.R;
import com.google.android.material.imageview.ShapeableImageView;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link Fragment_Mine#newInstance} factory method to
 * create an instance of this fragment.
 */
public class Fragment_Mine extends Fragment {

    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;

    public Fragment_Mine() {
        // Required empty public constructor
    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param param1 Parameter 1.
     * @param param2 Parameter 2.
     * @return A new instance of fragment Fragment_Mine.
     */
    // TODO: Rename and change types and number of parameters
    public static Fragment_Mine newInstance(String param1, String param2) {
        Fragment_Mine fragment = new Fragment_Mine();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    private ShapeableImageView img;
    private TextView name;
    private TableLayout tab1;
    private TableLayout tab2;
    private TableLayout tab3;
    private TableLayout tab4,download;



    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view=inflater.inflate(R.layout.fragment__mine, container, false);
        img = view.findViewById(R.id.img);
        name = view.findViewById(R.id.name);
        tab1 = view.findViewById(R.id.tab1);
        tab2 = view.findViewById(R.id.tab2);
        tab3 = view.findViewById(R.id.tab3);
        tab4 = view.findViewById(R.id.tab4);
        download= view.findViewById(R.id.download);
        tab1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                myinfo();
            }
        });
        tab2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent();
                intent.setClass(getContext() , ChangeUser.class);
                startActivity(intent);
            }
        });
        tab3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent();
                intent.setClass(getContext() , NoticeList.class);
                startActivity(intent);
            }
        });
        download.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent();
                intent.setClass(getContext() , MyDownload.class);
                startActivity(intent);
            }
        });
        tab4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                System.exit(0);
            }
        });
        name.setText(MainActivity.user.getName());
        return view;
    }

    @Override
    public void onStart() {
        super.onStart();
        name.setText(MainActivity.user.getName());
        img.setImageDrawable(MainActivity.user.getPic());
    }
    void myinfo(){
        AlertDialog.Builder dialog = new AlertDialog.Builder (getContext());
        dialog.setTitle("个人信息");//设置对话框的标题
        String s="";
        s+="\n\n账号："+MainActivity.user.getName()+"\n\n";
        s+="邮箱："+MainActivity.user.getEmail()+"\n\n";
        s+="身份："+MainActivity.user.getType()+"\n\n";
        s+="密码："+MainActivity.user.getPassword()+"";
        dialog.setMessage(s);//设置对话框的内容
        dialog.setCancelable(false);//设置对话框是否可以取消
        dialog.setIcon(R.drawable.myinfo);
        dialog.setPositiveButton("确定", new DialogInterface. OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {

            }
        });

        dialog.show();
    }
}